package com.atguigu.gmall.product.util;

import org.apache.commons.io.FilenameUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.*;

import java.io.IOException;

public class FileUploadUtil {
    private static StorageClient1 storageClient1;
    static {
        try {
            String file = FileUploadUtil.class.getResource("/tracker.conf").getFile();
            ClientGlobal.init(file);
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageServer storageServer = null;
            storageClient1 = new StorageClient1(trackerServer, storageServer);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public synchronized static String doFileUpload(String originalFileName, byte[] fileBytes) {

        try {
            String extension = FilenameUtils.getExtension(originalFileName);
            String fileUrl = storageClient1.upload_appender_file1(fileBytes, extension, null);
            return fileUrl;
        } catch (Exception e) {
           throw  new RuntimeException(e);
        }
    }

}
